<script setup>
import axios from 'axios';
import { onMounted, ref, watch } from 'vue';
import { defineProps } from 'vue';

const props = defineProps({
    information: {
        type: Object,
        required: true
    }
});

const itemList = ref([]);

// 获取数据
const fetchData = (url) => {
    axios.get(url)
        .then(response => {
            itemList.value = response.data;
        })
        .catch(error => {
            console.error('Error fetching data:', error);
        });
};

onMounted(() => {
    fetchData(props.information.url);
});

watch(() => props.information.url, (newUrl) => {
    fetchData(newUrl);
});
</script>

<template>
    <!-- 瀑布流卡片布局 -->
    <div class="masonry-container">
        <div 
            v-for="(item, index) in itemList" 
            :key="item.id" 
            class="masonry-card"
            :class="`card-${(index % 4) + 1}`" 
        >
            <div class="card-image-wrapper">
                <img 
                    :src="item.imageUrl" 
                    :alt="item.name || 'Image'" 
                    class="card-image" 
                    loading="lazy"
                />
                <div class="card-badge">#{{ item.id }}</div>
            </div>
            <div class="card-content">
                <h3 class="card-title">{{ item.name }}</h3>
                <p class="card-desc" v-if="item.description">{{ item.description }}</p>
                <div class="card-footer">
                    <span class="card-tag" v-if="item.tag">{{ item.tag }}</span>
                    <button class="card-action">查看详情</button>
                </div>
            </div>
        </div>
    </div>
</template>

<style lang="css" scoped>
.masonry-container {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 24px;
    width: 100%;
    max-width: 1200px;
    margin: 0 auto;
    padding: 20px;
    box-sizing: border-box;
}

.masonry-card {
    background: white;
    border-radius: 16px;
    overflow: hidden;
    box-shadow: 0 6px 18px rgba(0, 0, 0, 0.08);
    transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
    display: flex;
    flex-direction: column;
    
    &:hover {
        transform: translateY(-8px);
        box-shadow: 0 12px 24px rgba(0, 0, 0, 0.15);
    }
}

/* 4种不同卡片样式 */
.card-1 {
    .card-image-wrapper {
        aspect-ratio: 16/9;
    }
    .card-content {
        background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
    }
}

.card-2 {
    .card-image-wrapper {
        aspect-ratio: 1/1;
    }
    .card-content {
        background: linear-gradient(135deg, #e0f7fa 0%, #b2ebf2 100%);
    }
}

.card-3 {
    .card-image-wrapper {
        aspect-ratio: 4/3;
    }
    .card-content {
        background: linear-gradient(135deg, #fce4ec 0%, #f8bbd0 100%);
    }
}

.card-4 {
    .card-image-wrapper {
        aspect-ratio: 3/4;
    }
    .card-content {
        background: linear-gradient(135deg, #e8f5e9 0%, #c8e6c9 100%);
    }
}

.card-image-wrapper {
    position: relative;
    width: 100%;
    overflow: hidden;
}

.card-image {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: transform 0.5s ease;
    
    .masonry-card:hover & {
        transform: scale(1.05);
    }
}

.card-badge {
    position: absolute;
    top: 12px;
    left: 12px;
    background: rgba(0, 0, 0, 0.7);
    color: white;
    padding: 4px 10px;
    border-radius: 20px;
    font-size: 12px;
    font-weight: 500;
}

.card-content {
    padding: 18px;
    flex-grow: 1;
    display: flex;
    flex-direction: column;
}

.card-title {
    font-size: 18px;
    font-weight: 600;
    margin: 0 0 10px;
    color: #333;
}

.card-desc {
    font-size: 14px;
    color: #666;
    margin: 0 0 15px;
    line-height: 1.5;
    flex-grow: 1;
}

.card-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: auto;
}

.card-tag {
    font-size: 12px;
    background: rgba(0, 0, 0, 0.1);
    padding: 4px 10px;
    border-radius: 20px;
    color: #555;
}

.card-action {
    background: #4a6bdf;
    color: white;
    border: none;
    padding: 8px 16px;
    border-radius: 20px;
    font-size: 14px;
    cursor: pointer;
    transition: all 0.3s ease;
    
    &:hover {
        background: #3a56c0;
        transform: translateY(-2px);
    }
}

/* 响应式调整 */
@media (max-width: 1024px) {
    .masonry-container {
        grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
        gap: 20px;
    }
}

@media (max-width: 768px) {
    .masonry-container {
        grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
        gap: 16px;
        padding: 15px;
    }
    
    .card-title {
        font-size: 16px;
    }
    
    .card-content {
        padding: 14px;
    }
}

@media (max-width: 480px) {
    .masonry-container {
        grid-template-columns: 1fr;
    }
}
</style>